<?php

namespace App\Models\Mini;

use App\Models\GoodsClass;
use App\Models\Property;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;

class FleaGoodsModel extends Model
{
    protected $table = 'flea_goods';

    public $timestamps = false;

    protected $appends = ['status_text','type_text','sale_text','cate_name','estate_name'];

    /**
     * @Notes 创建时间的时间格式
     * @param $value
     * @return false|string
     * @author ypp
     * @date 2021/3/8
     * @time 10:47
     */
    public function getCreatedAtAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }


    /**
     * @Notes 返回图片
     * @param $value
     * @return false|string[]
     * @author ypp
     * @date 2021/3/8
     * @time 10:48
     */
    public function getGoodsImagesAttribute($value)
    {
        $arr = explode(',', $value);
        return $arr;
    }

    /**
     * @Notes 获取交易方式
     * @return string
     * @author ypp
     * @date 2021/3/8
     * @time 10:48
     */
    public function getTypeTextAttribute()
    {
        if ($this->deal_type == 1) {
            return '自提';
        }
        if ($this->deal_type == 2) {
            return '面交';
        }

        return '其它';

    }
    /**
     * @Notes 获取卖出状态
     * @return string
     * @author ypp
     * @date 2021/3/8
     * @time 10:48
     */
    public function getSaleTextAttribute()
    {
        switch ($this->is_sale){
            case 0:
                $str =  '未卖出';
                break;
            case 1:
                $str = '已卖出';
                break;
            case 2:
                $str= '用户拍下代付款';
                break;
            case 3:
                $str= '物业删除';
                break;
            case 4:
                $str= '平台删除';
                break;
            default:
                $str= '其他';

        }

        return $str;

    }

    /**
     * Notes: 获取状态描述
     * User: lstink
     * Date: 2021/3/4
     * Time: 6:40 下午
     * @return string
     */
    public function getStatusTextAttribute()
    {
        if ($this->status == 0) {
            return '待处理';
        }
        if ($this->status == 1) {
            return '处理中';
        }
        if ($this->status == 2) {
            return '已完成';
        }

        return '其它';
    }

    /**
     * @Notes 获取分类
     * @return mixed
     * @author ypp
     * @date 2021/3/8
     * @time 14:09
     */
    public function getCateNameAttribute()
    {
        $name = GoodsClass::where(['id'=>$this->cate_id])->value('name');
        return $name;
    }

    /**
     * @Notes 关联用户
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * @author ypp
     * @date 2021/3/9
     * @time 18:37
     */
    public function user_info()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    /**
     * 物业名称
     * @param $value
     * @return false|string
     */
    public function getEstateNameAttribute()
    {
        $id = explode(',',$this->attributes['estate_id'] ?? '');
        if($id){
            $name = Property::where(['id'=>$id])->value('name');
        }
        return $name ?? '';
    }

}
